
*ASSIGN PATH HERE
global replication "C:\Users\asufi\Dropbox (Chicago Booth)\shared\MianSufi\ncdfromscratch201903\replication"

**FIGURE 1
**Panel a
use "$replication\Fig1a.dta", clear
# delimit ;
graph twoway line ratio ratio_subprime ratio_alta year, lc(maroon navy dkgreen) lp(solid dash longdash) lw(thick thick thick)
xlabel(, labsize(large)) 
ylabel(,labsize(large) axis(1))
xtitle("", size(large)) 
ytitle("Ratio of Total Mortgage Originations", size(medium) axis(1))
title("PLS Fraction of Originations", size(huge)) plotregion(lstyle(none))
legend(size(medium) rows(1) region(lwidth(none))) 
name(riseratio, replace);
# delimit cr
*Panel b
use "$replication\Fig1b.dta", clear
# delimit ;
graph twoway line spread month if year(dofm(month))>=2002 & year(dofm(month))<=2005, lc(maroon) lp(solid) lw(thick)
xlabel(, labsize(large)) 
ylabel(,labsize(large) axis(1))
xtitle("", size(large)) 
ytitle("PLS to Treasury Spread (residualized)", size(medium) axis(1))
title("PLS to Treasury Spread (residualized)", size(huge)) plotregion(lstyle(none))
legend(size(large) rows(1) region(lwidth(none)))
name(fallspread, replace);
# delimit cr
#delimit ;
graph combine riseratio  fallspread,
col(2) xsize(12) ysize(6);
#delimit cr

**FIGURE 2
use "$replication\Fig2.dta", clear
# delimit ;
graph twoway line lgovsec_1 totprivsec_1 lonbal_1 year,
lc(navy maroon black) lp(dash solid shortdash) lw(thick thick thick)
xlabel(, labsize(large)) 
ylabel(,labsize(large) axis(1))
xtitle("", size(large)) 
ytitle("Millions in US $, relative to 2000", size(large))
legend(size(large) rows(2) region(lwidth(none)));
# delimit cr

**FIGURE 3
use "$replication\Fig3.dta", clear
# delimit ;
graph bar (mean) hmda if type=="Total"  ,  over(large, ) over(categ) asyvars  
legend(region(lwidth(none)) size(large)) bar(1, color(black)) bar(2, color(gs11))    graphregion(color(white)) 
bgcolor(white) plotregion(lstyle(none)) ytitle("Fraction of total", size(large)) ylab(, nogrid labs(large));
# delimit cr

**FIGURE 4
use "$replication\Fig4.dta", clear
*panel a
#delimit ;
graph twoway (scatter Ihmda1 Ihmda0 year if year>=2000 & year<=2010, 
connect(l l ) lp(solid dash) lw(thick thick) lc(maroon navy) msymbol(i i) mcolor(navy maroon) ),
title("All mortgage originations")
ytitle("Index: 2002=100")
name(total, replace)
xtitle("") legend(order(1 "High NCL ratio" 2 "Low NCL ratio") region(lwidth(none))) plotregion(lstyle(none)) ; 
#delimit cr
*panel b
#delimit ;
graph twoway (scatter Ilapproved_11 Ilapproved_10 year if year>=2000 & year<=2010, 
connect(l l ) lp(solid dash) lw(thick thick) lc(maroon navy) msymbol(i i) mcolor(navy maroon) ),
title("Home purchase mortgage originations")  
xtitle("") name(purch,replace) legend(order(1 "High NCL Ratio" 2 "Low NCL Ratio") region(lwidth(none))) plotregion(lstyle(none));
#delimit cr
#delimit ;
graph combine total purch,
col(2) xsize(10) ysize(5);
#delimit cr

**FIGURE 5
*panel a
use "$replication\Fig5a.dta", clear
areg hmda_log /*
	*/ c.ncl02##ib2002.year [aw=hmda2002], a(bhc_hud_id) vce(r)
	
capture drop lb coef ub year_ci	
qui gen lb = .
qui gen coef = . 
qui gen ub = .
qui gen year_ci = .

local nn = 1
forvalues yy = 2000/2010 {
	qui replace year_ci = `yy' if _n == `nn'
	qui replace lb = _b[`yy'.year#c.ncl02] - 1.96*_se[`yy'.year#c.ncl02] if _n == `nn'
	qui replace coef = _b[`yy'.year#c.ncl02] if _n == `nn'
	qui replace ub = _b[`yy'.year#c.ncl02] + 1.96*_se[`yy'.year#c.ncl02] if _n == `nn'
	local nn = `nn' + 1
}

lab var lb "Lower Bound CI"
lab var ub "Upper Bound CI"
lab var coef "ncl02_label_l" 
lab var year_ci "Year"	

graph twoway (rcap lb ub year_ci if year_ci>=1998 & year_ci<=2010, /*
	*/ title("Lender-level first stage") xtitle("") /*
	*/ ytitle("{&beta}{sub:k}") legend(off) /*
	*/ name(bankcoefleft, replace) plotregion(lstyle(none)) /*
	*/ yline(0)) (connected coef year_ci if year_ci>=2000 & year_ci <=2010, /*
	*/ ylabel(-1.5 -1 -0.5 0 0.5 1 1.5))
	
*panel b
use "$replication\Fig5b_1.dta", clear
qui gen year_ci = .
qui gen coeftract = . 
qui gen lbtract = .
qui gen ubtract = .
local nn = 1
forval yy=2000/2010 {
reghdfe  Dhmda`yy' ncl02 [aw=wt] , absorb(fipstractn) vce(cluster fipstractn bhc_hud_id_num)
qui replace year_ci = `yy' if _n == `nn'
qui replace coeftract = _b[ncl02] if _n == `nn'
qui replace lbtract = _b[ncl02] - 1.96*_se[ncl02] if _n == `nn'
qui replace ubtract = _b[ncl02] + 1.96*_se[ncl02] if _n == `nn'
local nn = `nn' + 1
}
drop if year_ci==.
keep coeftract year_ci lbtract ubtract   
lab var coeftract "Coefficient Estimate, Census tract Level" 
lab var year_ci "Year"
tempfile tract
save `tract', replace
use "$replication\Fig5b_2.dta", clear
qui gen year_ci = .
qui gen coefmsa = . 
qui gen lbmsa = .
qui gen ubmsa = .
local nn = 1
forval yy=2000/2010 {
reghdfe  Dhmda`yy' ncl02 [aw=wt] , absorb(cbsa_code) vce(cluster cbsa_code bhc_hud_id_num)
qui replace year_ci = `yy' if _n == `nn'
qui replace coefmsa = _b[ncl02] if _n == `nn'
qui replace lbmsa = _b[ncl02] - 1.96*_se[ncl02] if _n == `nn'
qui replace ubmsa = _b[ncl02] + 1.96*_se[ncl02] if _n == `nn'
local nn = `nn' + 1
}
drop if year_ci==.
keep coefmsa year_ci lbmsa ubmsa   
lab var coefmsa "Coefficient Estimate, cbsa Level" 
lab var year_ci "Year"
tempfile msa
save `msa', replace
use "$replication\Fig5b_3.dta", clear
areg Dhmda /*
	*/ c.ncl02##ib2002.year [aw=hmda2002], a(bhc_hud_id) vce(r)
capture drop lb coef ub year_ci	
qui gen lb = .
qui gen coef = . 
qui gen ub = .
qui gen year_ci = .

local nn = 1
forvalues yy = 2000/2010 {
	qui replace year_ci = `yy' if _n == `nn'
	qui replace lb = _b[`yy'.year#c.ncl02] - 1.96*_se[`yy'.year#c.ncl02] if _n == `nn'
	qui replace coef = _b[`yy'.year#c.ncl02] if _n == `nn'
	qui replace ub = _b[`yy'.year#c.ncl02] + 1.96*_se[`yy'.year#c.ncl02] if _n == `nn'
	local nn = `nn' + 1
}
keep if year_ci~=.
sort year_ci
merge 1:1 year_ci using `msa'
drop _merge
sort year_ci
merge 1:1 year_ci using `tract'
drop _merge
label var coef "Baseline"
label var coefmsa "MSA-by-year FE"
label var coeftract "Tract-by-year FE"
graph twoway connected coef coefmsa coeftract year_ci if year_ci>=2000 & year_ci <=2010, /*
	*/ title("Khwaja-Mian specification") xtitle("") /*
	*/ ytitle("{&beta}{sub:k}") ylabel(-1.5 -1 -0.5 0 0.5 1 1.5) /*
	*/ name(bankcoefright, replace) plotregion(lstyle(none)) /*
	*/ yline(0) 
#delimit ;
graph combine bankcoefleft bankcoefright,
col(2) xsize(10) ysize(5);
#delimit cr


**FIGURE 7
use "$replication\Fig7.dta", clear
areg hmdahome i.datem c.ncl02#i.month, a(cbsa_code)
predict hmdahomeSA, residuals
areg hmdahomeSA c.ncl02##ib515.datem [aweight=totalocc2000] , a(cbsa_code) vce(r) 
	capture drop coef datem_ci	
qui gen coef = . 
qui gen datem_ci = .
forvalues yy = 480/611 {
	qui replace datem_ci = `yy' if _n == `yy'-479
	qui replace coef = _b[`yy'.datem#c.ncl02] if _n == `yy'-479
}
format %tm datem_ci
lab var coef "Coefficient estimate" 
lab var datem_ci "Date"	
graph twoway scatter coef datem_ci, /*
	*/  xtitle("") legend(off) ytitle("{&beta}{sub:k}", size(large)) /*
	*/ xlabel(,labsize(large)) ylabel(,labsize(large)) /*
	*/ name(geomthreg, replace) c(l) lp(dash) lw(thin) lc(black) mc(blue) m(C) msize(vsmall) /*
	*/ yline(0) plotregion(lstyle(none))
	
format %tm datem_ci
lab var coef "Coefficient estimate (left axis)" 
lab var datem_ci "Date"	
keep datem_ci coef
keep if datem_ci~=.
gen month=datem_ci
sort month
merge 1:1 month using "$replication\Fig7_add"
sort month
format month %tm
keep if month>=509 & month<=533
label var spread "PLS spread to Treasury residual (right axis)"

# delimit ;
graph twoway (scatter coef datem_ci , 
m() msize(vsmall) mc(blue) c(l) lp(dash) lw(thin) lc(black)) 
(scatter spread month, c(l) m(Th) mc(red) msize(vsmall) lp(solid)  lw(thin) lc(black) yaxis(2)), 
xlabel(, labsize(large)) 
ylabel(,labsize(large) axis(1))
ylabel(,labsize(large) axis(2))
xtitle("", size(large)) 
ytitle("{&beta}{sub:k}", size(large) axis(1))
ytitle("PLS spread to" "Treasury residual", size(large) axis(2))
legend(size(medium) rows(2) region(lwidth(none))) name(geomthregshort, replace) plotregion(lstyle(none)) ;
# delimit cr
#delimit ;
graph combine geomthreg geomthregshort,
col(2) xsize(16) ysize(8);
#delimit cr


**FIGURE 8
use "$replication\Fig7.dta", clear
areg hmdarefi i.datem c.ncl02#i.month, a(cbsa_code)
predict hmdarefiSA, residuals
areg hmdarefiSA c.ncl02##ib515.datem [aweight=totalocc2000] , a(cbsa_code) vce(r) 
	capture drop coef datem_ci	
qui gen coef = . 
qui gen datem_ci = .
forvalues yy = 480/611 {
	qui replace datem_ci = `yy' if _n == `yy'-479
	qui replace coef = _b[`yy'.datem#c.ncl02] if _n == `yy'-479
}
format %tm datem_ci
lab var coef "Coefficient estimate" 
lab var datem_ci "Date"	
graph twoway scatter coef datem_ci, /*
	*/  xtitle("") legend(off) ytitle("{&beta}{sub:k}", size(large)) /*
	*/ xlabel(,labsize(large)) ylabel(,labsize(large)) /*
	*/ name(geomthreg, replace) c(l) lp(dash) lw(thin) lc(black) mc(blue) m(C) msize(vsmall) /*
	*/ yline(0) plotregion(lstyle(none))
	
format %tm datem_ci
lab var coef "Coefficient estimate (left axis)" 
lab var datem_ci "Date"	
keep datem_ci coef
keep if datem_ci~=.
gen month=datem_ci
sort month
merge 1:1 month using "$replication\Fig7_add"
sort month
format month %tm
keep if month>=509 & month<=533
label var spread "PLS spread to Treasury residual (right axis)"

# delimit ;
graph twoway (scatter coef datem_ci , 
m() msize(vsmall) mc(blue) c(l) lp(dash) lw(thin) lc(black)) 
(scatter spread month, c(l) m(Th) mc(red) msize(vsmall) lp(solid)  lw(thin) lc(black) yaxis(2)), 
xlabel(, labsize(large)) 
ylabel(,labsize(large) axis(1))
ylabel(,labsize(large) axis(2))
xtitle("", size(large)) 
ytitle("{&beta}{sub:k}", size(large) axis(1))
ytitle("PLS spread to" "Treasury residual", size(large) axis(2))
legend(size(medium) rows(2) region(lwidth(none))) name(geomthregshort, replace) plotregion(lstyle(none)) ;
# delimit cr

#delimit ;
graph combine geomthreg geomthregshort,
col(2) xsize(16) ysize(8);
#delimit cr

*FIGURE 9
use "$replication\Fig9.dta", clear
foreach var of varlist volcl hpflhmda{
	local l`var': var lab `var'
	*di "`l`var''"
}
foreach var of varlist volcl hpflhmda{
areg `var'_log /*
	*/ c.ncl02##ib2002.year [aweight=totalocc2000], a(zipcode) cluster(cbsa_code)
	
capture drop coef year_ci lb ub	

qui gen coef = . 
qui gen year_ci = .
qui gen lb = .
qui gen ub = .

local nn = 1
forvalues yy = 2000/2010 {
	qui replace year_ci = `yy' if _n == `nn'
	qui replace lb = _b[`yy'.year#c.ncl02] - 1.96*_se[`yy'.year#c.ncl02] if _n == `nn'
	qui replace coef = _b[`yy'.year#c.ncl02] if _n == `nn'
	qui replace ub = _b[`yy'.year#c.ncl02] + 1.96*_se[`yy'.year#c.ncl02] if _n == `nn'
	local nn = `nn' + 1
}

lab var coef "Coefficient Estimate" 
lab var year_ci "Year"	
lab var lb "Lower Bound CI"
lab var ub "Upper Bound CI"


graph twoway (rcap lb ub year_ci, /*
	*/ title("`l`var''", size(huge)) legend(off) plotregion(lstyle(none)) /*
	*/ name("`var'gc", replace) xlabel(, labs(large))  ylabel(-4(1)3, labs(large))  /*
	*/ yline(0)) (connected coef year_ci, legend(off) ytitle("{&beta}{sub:k}", size(large)) xtitle(""))
drop coef year_ci lb ub
}
*
#delimit ;
graph combine hpflhmdagc volclgc,
col(2) xsize(10) ysize(5) ycommon;
#delimit cr

*FIGURE 11
use "$replication\Fig11a.dta", clear
areg lunit /*
	*/ c.ncl02##ib2002.year i.year#c.elasticity [aweight=totalocc2000], a(cbsa_code) cluster(cbsa_code)
	
capture drop coef year_ci lb ub	
qui gen coef = . 
qui gen year_ci = .
qui gen lb = .
qui gen ub = .

local nn = 1
forvalues yy = 2000/2010 {
	qui replace year_ci = `yy' if _n == `nn'
	qui replace lb = _b[`yy'.year#c.ncl02] - 1.96*_se[`yy'.year#c.ncl02] if _n == `nn'
	qui replace coef = _b[`yy'.year#c.ncl02] if _n == `nn'
	qui replace ub = _b[`yy'.year#c.ncl02] + 1.96*_se[`yy'.year#c.ncl02] if _n == `nn'
	local nn = `nn' + 1
}
lab var coef "Coefficient Estimate" 
lab var year_ci "Year"	
lab var lb "Lower Bound CI"
lab var ub "Upper Bound CI"
graph twoway (rcap lb ub year_ci, /*
	*/ title("Housing Units Constructed", size(huge)) legend(off)  /*
	*/ name("Aggrlunitgc", replace)  xlabel(, labs(large))  /*
	 */ ytitle("{&beta}{sub:k}", size(large)) xtitle("") plotregion(lstyle(none)) /*
	*/ yline(0)) (connected coef year_ci, legend(off)) 
	
use "$replication\Fig11b.dta", clear
foreach var of varlist cl{
areg `var'_log /*
	*/ c.ncl02##ib2002.year i.year#c.elasticity [aweight=totalocc2000] if year>=2000 & year<=2010, a(zipcode) cluster(cbsa_code)
	
capture drop coef year_ci lb ub	
qui gen coef = . 
qui gen year_ci = .
qui gen lb = .
qui gen ub = .

local nn = 1
forvalues yy = 2000/2010 {
	qui replace year_ci = `yy' if _n == `nn'
	qui replace lb = _b[`yy'.year#c.ncl02] - 1.96*_se[`yy'.year#c.ncl02] if _n == `nn'
	qui replace coef = _b[`yy'.year#c.ncl02] if _n == `nn'
	qui replace ub = _b[`yy'.year#c.ncl02] + 1.96*_se[`yy'.year#c.ncl02] if _n == `nn'
	local nn = `nn' + 1
}

lab var coef "Coefficient Estimate" 
lab var year_ci "Year"	
lab var lb "Lower Bound CI"
lab var ub "Upper Bound CI"

graph twoway (rcap lb ub year_ci, /*
	*/ title("Housing Prices", size(huge)) legend(off) plotregion(lstyle(none)) /*
	*/ name("`var'gc", replace)  xlabel(, labs(large))  /*
	 */ ytitle("{&beta}{sub:k}", size(large)) xtitle("") /*
	*/ yline(0)) (connected coef year_ci, legend(off)) 
}
#delimit ;
graph combine clgc Aggrlunitgc,
col(2) xsize(10) ysize(5);
#delimit cr

*FIGURE 13
use "$replication\Fig_1314_Michigandata", clear
collapse (mean) fav* unfav* *_*buy [aw=WT], by(year)
label var year ""
label var unfav "Bad time to buy"
label var unfav_price "Bad time to buy, prices"
label var unfav_pricehigh "Bad time to buy, prices too high"
label var unfav_pricefall "Bad time to buy, prices will fall"
label var fav_price "Good time to buy, prices"
label var fav "Good time to buy"
keep if year<=2010
#delimit ;
twoway line unfav unfav_price unfav_pricefall unfav_pricehigh year,yaxis(1) lp(solid solid solid solid) lw(thick thick thick thick) lc(black maroon dkorange dkgreen) lp(solid dash longdash shortdash)  plotregion(lstyle(none))
ytitle("Bad time to buy a house, fraction", axis(1) size(medsmall)) xtitle("")
xlabel(2000(2)2010, labs(medium)) ylabel(, labs(medium)) legend(rows(4)) title("Rising pessimism", size(huge)) name(figbel1, replace)  ;
#delimit cr
#delimit ;
twoway line fav fav_price year,yaxis(1) lp(solid solid) lw(thick thick) lc(navy lavender) lp(solid dash)  plotregion(lstyle(none))
ytitle("Good time to buy, prices, fraction", axis(1) size(medsmall)) xtitle("")
xlabel(2000(2)2010, labs(medium)) ylabel(, labs(medium)) legend(rows(2)) title("Cluster of optimists", size(huge)) name(figbel2, replace)  ;
#delimit cr
#delimit ;
graph combine figbel1 figbel2,
col(2) xsize(10) ysize(5);
#delimit cr

**FIGURE 14
use "$replication\Fig_1314_Michigandata", clear
collapse (mean) fav* unfav* *_*buy [aw=WT], by(year)
label var year ""
label var unfav "Bad time to buy"
label var unfav_price "Bad time to buy, prices"
label var unfav_pricehigh "Bad time to buy, prices too high"
label var unfav_pricefall "Bad time to buy, prices will fall"
label var fav_price "Good time to buy, prices"
label var fav "Good time to buy"
keep if year<=2010
keep year unfav* fav*
sort year
tempfile michagg
save `michagg', replace
use "$replication\Fig_1314_Michigandata", clear
collapse (firstnm) County=COUNTY_NAME (mean) fav* unfav* count [aw=WT], by(year fips)
merge 1:1  year fips using "$replication\Fig_1314_ShillerBeliefsAgg"
drop if _merge==1
drop _merge
foreach var of varlist WINAVGavgexp unfav_price {
areg `var', absorb(fips)
predict `var'dm, resid
}
#delimit ;
graph twoway scatter WINAVGavgexpdm unfav_pricedm, 
mlabel(year) mlabsize(small) legend(off) ytitle("Home-buyer house price expectation", size(medium))
xtitle("Fraction of population saying bad time to buy due to prices", size(medium))
xlabel(, labs(medium)) ylabel(, labs(medium))
|| 
lfit WINAVGavgexpdm unfav_pricedm, lc(red) name(figbel1dm, replace);
#delimit cr
collapse (mean) *exp*, by(year)
sort year 
merge 1:1 year using `michagg'
keep if _merge==3
drop _merge
label var WINAVGtenyrexp "Mean of Long Term Beliefs (Left Axis)"
label var WINAVGoneyrexp "Mean of Short Term Beliefs (Left Axis)"
label var WINAVGavgexp "Average house price growth belief of home-buyers"
label var fav_price "Good time to buy due to prices"
label var unfav_price "Bad time to buy due to prices, fraction"
labe var year "Year"
corr unfav_price WINAVGavgexp
local rhoB : di %6.2f r(rho)
#delimit ;
twoway (line WINAVGavgexp year,yaxis(1) lp(dash) lw(thick) lc(navy) 
ytitle("Average house price growth belief of home-buyers", axis(1) size(medium)  ) xtitle("") )
(line unfav_price year, yaxis(2) lp(solid) lw(thick) lc(maroon)  
ytitle("Bad time to buy, prices, fraction", axis(2)  size(medium)  ) ), 
 plotregion(lstyle(none))   xlabel(2003(2)2011, labs(medium)) ylabel(,labs(medium)) legend(size(medium) rows(2) region(lwidth(none))) name(figbel2, replace)  ;
#delimit cr

#delimit ;
graph combine figbel2 figbel1dm,
col(2) xsize(8) ysize(3);
#delimit cr


**TABLE 1
use "$replication\Table1banklevel", clear
regress hmda0205 ncl_share_alt2002 [aweight=hmda2002], r
regress lprivsec0205i ncl_share_alt2002 [aweight=hmda2002], r
regress lgovsec0205i ncl_share_alt2002 [aweight=hmda2002], r
regress lonbal0205i ncl_share_alt2002 [aweight=hmda2002], r
use "$replication\Table1bankmsalevel", clear
reghdfe hmda0205 ncl_share_alt2002 [aw=wt] , absorb(cbsa_code) vce(cluster cbsa_code bhc_hud_id_num)
use "$replication\Table1banktractlevel", clear
reghdfe hmda0205 ncl_share_alt2002 [aw=wt] , absorb(fipstractn) vce(cluster fipstractn bhc_hud_id_num)

**TABLE 4
use "$replication\Table4"
regress volcl_pcch ncl02 [aweight=totalocc2000], cluster(cbsa_code) robust
regress volcl1__pcch ncl02 [aweight=totalocc2000], cluster(cbsa_code) robust
regress volcl2__pcch ncl02 [aweight=totalocc2000], cluster(cbsa_code) robust
regress volcl3__pcch ncl02 [aweight=totalocc2000], cluster(cbsa_code) robust

**TABLE 5
use "$replication\Table5zip", clear
regress cl0206 ncl02 elasticity [aweight=totalocc2000] if year==2002, cluster(cbsa_code) r
regress cl0610 ncl02 elasticity [aweight=totalocc2000] if year==2002, cluster(cbsa_code) r
regress cl0210 ncl02 elasticity [aweight=totalocc2000] if year==2002, cluster(cbsa_code) r
use "$replication\Table5cbsa", clear
regress unitsboom ncl02 elasticity [aweight=totalocc2000] if year==2002, robust
regress unitsbust ncl02 elasticity [aweight=totalocc2000] if year==2002, robust
regress unitslong ncl02 elasticity [aweight=totalocc2000] if year==2002, robust

**TABLE 6
use "$replication\Table6", clear
reg unfavdiff cl0206 if year==2002 [aweight=WT2], r
ivreg unfavdiff (cl0206=ncl02) if year==2002 [aweight=WT2], r
reg unfav_pricediff cl0206 if year==2002 [aweight=WT2], r
ivreg unfav_pricediff (cl0206=ncl02) if year==2002 [aweight=WT2], r
